import requests
import pymysql


class TxzpSpider:
    url = 'https://careers.tencent.com/tencentcareer/api/post/Query?\
    timestamp=1728007701507\
    &countryId=&cityId=&bgIds=&productId=&categoryId=&parentCategoryId=&attrId\
    =&keyword=go&pageIndex={}&pageSize=10&language=zh-cn&area=cn'

    headers = {
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/129.0.0.0 Safari/537.36'
    }

    def __init__(self):
        self.db = pymysql.connect(host='localhost', port=3306, user='root', passwd='888888', db='test')
        self.cursor = self.db.cursor()

    @classmethod
    def get_info(cls):
        for page in range(1, 11):
            resp = requests.get(url=cls.url.format(page), headers=cls.headers)
            print(f'正在抓取第{page}页')
            data = resp.json()['Data']['Posts']
            yield data

    def create_table(self):
        sql = """
            create table if not exists tx_works(
                id int primary key auto_increment,
                work_name varchar(100) not null,
                country varchar(50) ,
                city varchar(50) ,
                work_desc text not null
            );
        
        """
        try:
            self.cursor.execute(sql)
            print('表创建成功')
        except Exception as e:
            print(e, "创建失败")

    def insert_data(self, *args):
        """
        :param args:
            id
            work_name
            country
            city
            work_desc
        :return:
        """
        sql = """
            insert into tx_works(
            id,
            work_name,
            country,
            city,
            work_desc
            )values (%s,%s,%s,%s,%s);
        """

        try:
            self.cursor.execute(sql, args)
            self.db.commit()
            print("插入数据成功")
        except Exception as e:
            self.db.rollback()
            print(e, "插入数据失败")

    def main(self):
        self.create_table()
        all_data = self.get_info()
        id = 0
        for d in all_data:
            for w in d:
                work_name = w['RecruitPostName']
                country = w['CountryName']
                city = w['LocationName']
                work_desc = w['Responsibility']

                self.insert_data(id, work_name, country, city, work_desc)

        self.db.close()


if __name__ == '__main__':
    t = TxzpSpider()
    t.main()
